package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.persistence.model.StuAct;
import org.springframework.ui.Model;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.cache.Cache;
import com.stylefeng.guns.common.constant.dictmap.RoleDict;
import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.model.Activity;
import com.stylefeng.guns.common.persistence.model.Role;
import com.stylefeng.guns.common.persistence.model.Student;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.cache.CacheKit;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.biz.service.ActService;
import com.stylefeng.guns.modular.biz.service.StuService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stu")
public class StudentsController extends BaseController {
    private static String PREFIX = "/biz/students";

    /**
     * 跳转到学生列表页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/students.html";
    }

    /**
     * 跳转到添加学生
     */
    @RequestMapping(value = "/add")
    public String userAdd(Model model) {
        List<Activity> list=actService.selectActs(null);
        model.addAttribute("actList",list);
        return PREFIX + "/add.html";
    }
    @Resource
    ActService actService;
    @Resource
    StuService stuService;

    /**
     * 获取学生列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String stuName, @RequestParam(required = false) String grade, @RequestParam(required = false) String school, @RequestParam(required = false) String major,
                       @RequestParam(required = false) String actName, @RequestParam(required = false) String actYear) {
        Map<String,Object> params=new HashMap();
        params.put("stuName",stuName);
        params.put("grade",grade);
        params.put("school",school);
        params.put("major",major);
        params.put("actName",actName);
        params.put("actYear",actYear);
        List<Map<String, Object>> students = this.stuService.selectStudents(params);
        for (Map<String,Object> stu : students){
            if(stu.get("sex").toString().equals("0"))
              stu.put("sex","女");
            else {
                stu.put("sex","男");
            }
            stu.put("actInfo",stu.get("ac_year")+"-"+stu.get("ac_name"));
        }
        return students;
    }

  /**
     * 学生新增
     */
    @RequestMapping(value = "/add_stu")
    @BussinessLog(value = "添加学生", key = "name")
    @ResponseBody
    public Tip add(@Valid Student student,@RequestParam(required = false) String acts, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        System.out.println("acts ----- "+acts);
        student.setStuid(null);
        List<StuAct> list = getStuActs(acts);
        this.stuService.insert(student,list);
        return SUCCESS_TIP;
    }

    /**
     * 跳转到修改学生
     */
    @RequestMapping(value = "/edit/{stuId}/{type}")
    public String toEdit(@PathVariable Integer stuId,@PathVariable Integer type, Model model) {
        if (ToolUtil.isEmpty(stuId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Student student=stuService.selectStuById(stuId);
        model.addAttribute("stu",student);
        //获取活动列表
        List<Activity> list=actService.selectActs(null);
        model.addAttribute("actList",list);
        //获取学生活动关联列表
        List<StuAct> stuActList=stuService.selectStuActByStu(student);
        model.addAttribute("saList",stuActList);

        LogObjectHolder.me().set(student);
        if(type==1)
         return PREFIX + "/edit.html";
        else{
            return PREFIX + "/view.html";
        }
    }

    /**
     * 学生修改
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Tip edit(@Valid Student student,  @RequestParam(required = false) String acts,BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        List<StuAct> list = getStuActs(acts);
        this.stuService.updateStudent(student,list);

        //删除缓存
        CacheKit.removeAll(Cache.CONSTANT);
        return SUCCESS_TIP;
    }

    /**
     * 构造学生活动关联列表
     * @param acts
     * @return
     */
    private List<StuAct> getStuActs(@RequestParam(required = false) String acts) {
        String[] actIdStr = acts.split(",");
        List<StuAct> list=new ArrayList<>();
        for (int i=0; i<actIdStr.length;i++){
            StuAct act = new StuAct();
            Integer acid=Integer.parseInt(actIdStr[i]);
            act.setAcid(acid);

            list.add(act);
        }
        return list;
    }

    /**
     * 删除学生
     */
    @RequestMapping(value = "/remove")
    @BussinessLog(value = "删除角色", key = "roleId")
    @ResponseBody
    public Tip remove(@Valid Student student) {
        if (ToolUtil.isEmpty(student)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.stuService.deleteStudents(student);
        //删除缓存
        CacheKit.removeAll(Cache.CONSTANT);
        return SUCCESS_TIP;
    }
}
